package com.bee2c.zas.home.dao;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.util.StringUtils;

import com.bee2c.pub.util.dao.BaseDao;
import com.bee2c.zas.entity.FbpManagementBody;

public class FbpManagementBodyDaoImpl extends BaseDao<FbpManagementBody> {

	List<FbpManagementBody> findListByCon(FbpManagementBody con) {
		Map<String, Object> paraMap = new HashMap<String, Object>();
		StringBuilder jql = new StringBuilder("select t from FbpManagementBody t where 1 = 1");
		if (!StringUtils.isEmpty(con.getManagementBodyCode())) {
			jql.append(" and managementBodyCode = :managementBodyCode");
			paraMap.put("managementBodyCode", con.getManagementBodyCode().trim());
		}
		if (!StringUtils.isEmpty(con.getManagementBodyName())) {
			jql.append(" and managementBodyName like :managementBodyName");
			paraMap.put("managementBodyName", "%" + con.getManagementBodyName().trim() + "%");
		}
		jql.append(" order by managementBodyId desc");
		List<FbpManagementBody> result = super.findAll(jql.toString(), paraMap);
		return result;
	}

	Page<FbpManagementBody> findPageByCon(FbpManagementBody con, int pageIndex, int pageSize) {
		Specification<FbpManagementBody> spec = new Specification<FbpManagementBody>() {
			@Override
			public Predicate toPredicate(Root<FbpManagementBody> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (con == null) {
					return null;
				}
				if (!StringUtils.isEmpty(con.getManagementBodyCode())) {
					list.add(cb.equal(root.get("managementBodyCode"), con.getManagementBodyCode().trim()));
				}
				if (!StringUtils.isEmpty(con.getManagementBodyName())) {
					list.add(cb.like(root.get("managementBodyName"), "%" + con.getManagementBodyName().trim() + "%"));
				}
				query.where(list.toArray(new Predicate[list.size()]));
				return null;
			}
		};
		Sort sort = new Sort(Direction.DESC, "managementBodyId");
		return super.findAll(spec, sort, pageIndex, pageSize);
	}
}
